#include "imagedisplay.h"

ImageDisplay::ImageDisplay()
{
    QSize size(640, 480);
    designedShape = null;

    image = QPixmap::fromImage(QImage("image/image.png")).scaled(size);

    this->setMaximumSize(size);
    this->setMinimumSize(size);
    this->resize(size);
}

void ImageDisplay::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.drawPixmap(0,0,640,480, image);

    QPen pen(Qt::DashLine);
    painter.setPen(pen);

    switch(designedShape)
    {
    case ROI :
        painter.drawRect(mousePosClic.x(), mousePosClic.y(), mousePosCurrent.x(), mousePosCurrent.y());
        break;

    case Rectangle :
        painter.drawRect(mousePosClic.x(), mousePosClic.y(), mousePosCurrent.x(), mousePosCurrent.y());
        break;

    case Circle :
        painter.drawEllipse(mousePosClic.x(), mousePosClic.y(), mousePosCurrent.x(), mousePosCurrent.y());
        break;

    case Line :
        painter.drawLine(mousePosClic.x(), mousePosClic.y(), mousePosCurrent.x()+mousePosClic.x(), mousePosCurrent.y()+mousePosClic.y());
        break;
    }
//    qDebug() << QString::number(mousePosClic.x());
//    qDebug() << QString::number(mousePosClic.y());
//    qDebug() << QString::number(mousePosCurrent.x());
//    qDebug() << QString::number(mousePosCurrent.y());
    "test svn"
}

void ImageDisplay::setDrawShape(ShapeType type)
{
    designedShape = type;
}

void ImageDisplay::setImage(Image img)
{
    QImage qimg = Image::getQImage(img.getImage());
    image = QPixmap::fromImage(qimg);

    this->repaint();
}

void ImageDisplay::imgMouseClic()
{
    mousePosClic = this->mapFromGlobal(QCursor::pos());
}

void ImageDisplay::imgMouseMove()
{
    mousePosCurrent = this->mapFromGlobal(QCursor::pos()) - mousePosClic;
    this->repaint();
}

DrawShape ImageDisplay::imgMouseRelease()
{
    mousePosCurrent = this->mapFromGlobal(QCursor::pos()) - mousePosClic;
    this->repaint();

    QRect rect(QPoint(std::min(mousePosClic.x(), mousePosCurrent.x()),
                      std::min(mousePosClic.y(), mousePosCurrent.y())),
               QPoint(std::max(mousePosClic.x(), mousePosCurrent.x()),
                      std::max(mousePosClic.y(), mousePosCurrent.y())));

    DrawShape shape(designedShape, rect);

    return shape;
}
